package com.xanthreal.gvalid;

import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

import javax.validation.Constraint;

public class AbstractValidationEngine {

	protected Annotation[] getAllConstraintedAnnotations(Annotation[] parameterAnnotations) {
		Set<Annotation> constraintedAnnotationSet = new HashSet<Annotation>();
		for (int i = 0; i < parameterAnnotations.length; i++) {
			Annotation annotation = parameterAnnotations[i];
			if (annotation.annotationType().isAnnotationPresent(Constraint.class)) {
				constraintedAnnotationSet.add(annotation);
			}
		}
		return constraintedAnnotationSet.toArray(new Annotation[constraintedAnnotationSet.size()]);
	}

	protected Validated getValidAnnotation(Annotation[] parameterAnnotations) {
		for (int i = 0; i < parameterAnnotations.length; i++) {
			Annotation annotation = parameterAnnotations[i];
			if (Validated.class.isAssignableFrom(annotation.getClass()))
				return (Validated) annotation;
		}
		return null;
	}

}